//
//  settingViewController.swift
//  WeChatDemo
//
//  Created by YJH on 2025/5/6.
//

import UIKit
import SnapKit

struct SetItem{
    let mineName: String
}

struct SetModel {
    let items : [SetItem]
    init() {
        self.items = [
            .init(mineName: "个人资料"),
            .init(mineName: "账号安全"),
            .init(mineName: "未成年模式"),
            .init(mineName: "关怀模式")
        ]
    }
}

extension Notification.Name {
    
    static let didMinorModeChange = Notification.Name("com.wechatdemo.didminormodechange")
    
}

class SetViewController : UIViewController{
    let data = SetModel()
    
    var isOnArray: [Bool] = []
    
    var callBack: (() -> Void) = {}

    var selectedMineItem: MineItem?
    
    lazy var tableView: UITableView = {
        let tableView = UITableView(frame: .zero, style: .grouped)
        tableView.register(SetTableViewCell.self, forCellReuseIdentifier: "cell")
        tableView.rowHeight = 44
        tableView.dataSource = self
        tableView.delegate = self
        tableView.sectionHeaderHeight = 5
        tableView.sectionFooterHeight = 0.01
        return tableView
    }()
    
    private func setupViews() {
        view.addSubview(tableView)
        tableView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupViews()
        navigationItem.title = "设置"
        isOnArray = Array(repeating: false, count: data.items.count - 2)
    }
    
    @objc private func handleSwitch(_ sender: UISwitch) {
        let index = sender.tag
        isOnArray[index] = sender.isOn
        if index == 0 {
            NotificationCenter.default.post(name: .didMinorModeChange, object: [
                "isOn": sender.isOn
            ])
        }
    }
    
}
// MARK: - table view data source
// 实现 UITableViewDataSource 和 UITableViewDelegate 协议方法
extension SetViewController: UITableViewDataSource, UITableViewDelegate {
    
    func numberOfSections(in tableView: UITableView) -> Int {
        2
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if section == 0 {
            return 2
        } else if section == 1 {
            return data.items.count - 2
        }
        return 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        guard let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as? SetTableViewCell
        else {
            return .init()
        }
        let itemIndex = indexPath.section == 0 ? indexPath.row : indexPath.row + 2
        let item = data.items[itemIndex]
        cell.setName.text = item.mineName
        
        if indexPath.section == 0 {
            cell.uiswitch.isHidden = true
            cell.accessoryType = .disclosureIndicator
        }
        
        if indexPath.section == 1 {
            cell.uiswitch.addTarget(self, action: #selector(handleSwitch(_:)), for: .valueChanged)
            cell.uiswitch.tag = indexPath.row
            
            cell.uiswitch.isOn = isOnArray[indexPath.row]
        }
        
        return cell
    }
}

class SetTableViewCell: UITableViewCell {
    
    lazy var setName: UILabel = {
        let label = UILabel()
        label.textColor = .darkGray
        label.font = .systemFont(ofSize: 14, weight: .medium)
        return label
    }()
    
    lazy var uiswitch: UISwitch = {
        let uiswitch = UISwitch()
        return uiswitch
    }()
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupViews()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupViews() {
        contentView.addSubview(setName)
        setName.snp.makeConstraints { make in
            make.centerY.equalTo(contentView)
            make.left.equalTo(contentView).offset(8)
        }
        
        contentView.addSubview(uiswitch)
        uiswitch.snp.makeConstraints { make in
            make.right.equalToSuperview().inset(10)
            make.centerY.equalToSuperview()
        }
    }
}
